package ui.activity.autogenerating_test_paper;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.AutoTestPaperAdapter;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.AutoTestPaperBean;
import bean.SingleQuestionBean;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.SPUtils;
import utils.StringUtils;
import view.PaperStructurePopupWindow;

/**
 * 智能组卷
 */
public class AutogeneratingTestPaperListActivity extends BaseNetActivity{
    private List<AutoTestPaperBean.mlist> list = new ArrayList();
    private List<AutoTestPaperBean> parentlist = new ArrayList();
    private List<SingleQuestionBean> totalList = new ArrayList<>();
    private AutoTestPaperAdapter adapter;
    private RecyclerView recyclerView;
    private TextView tv_paper_type;
    private TextView tv_paper_jiegou;
    private EditText ed_papername,
                     ed_time;
    private int degree_id,subject_id;
    private String textbook_chapter_id,lore_id;
    private String difficulty;
    private String source_type;
    private String questions;
    private Button bt_save;
    private View layoutview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("智能组卷");
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        hideLoadDialog();
        if(baseBean.code == 200){
            if(action.equals("Task/autoCreate")){
                parentlist = GsonUtils.getInstance(context).parseHaveHeaderJArray(baseBean,AutoTestPaperBean.class,"list");
                for(int i=0;i<parentlist.size();i++){
                    for(AutoTestPaperBean.mlist mlist:parentlist.get(i).list){
                       mlist.type_id = parentlist.get(i).type_id;
                    }
                    list.addAll(parentlist.get(i).list);
                }
                adapter.setNewData(list);
            }else if(action.equals("Task/savePaper")){
                finish();
            }
        }else {
            toast(baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {
        hideLoadDialog();
    }

    @Override
    protected int getLayoutId() {
        layoutview = View.inflate(context, R.layout.activity_autogeneratingtestpaper_list, null);
        return R.layout.activity_autogeneratingtestpaper_list;
    }

    @Override
    protected void findViews() {
        tv_paper_type = getView(R.id.tv_paper_type);
        tv_paper_jiegou = getView(R.id.tv_paper_jiegou);
        ed_papername = getView(R.id.ed_paper_name);
        ed_time = getView(R.id.ed_paper_time);
        bt_save = getView(R.id.bt_save);
        recyclerView = getView(R.id.rcly_);
    }

    @Override
    protected void formatViews() {
        bt_save.setOnClickListener(this);
        tv_paper_type.setOnClickListener(this);
        tv_paper_jiegou.setOnClickListener(this);
//        for(int i=0;i<10;i++){
//            TestPaperBean paperBean = new TestPaperBean();
//            List<TestPaperBean.Question_options> question_options = new ArrayList<>();
//            for(int j=0;j<4;j++){
//                TestPaperBean.Question_options options = new TestPaperBean.Question_options();
//                question_options.add(options);
//            }
//            paperBean.question_options = question_options;
//            list.add(paperBean);
//
//        }
        bt_save.setOnClickListener(this);
        LinearLayoutManager layoutManager =new LinearLayoutManager(context) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new AutoTestPaperAdapter(context,R.layout.item_testpaper,list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_up:
                        if (position > 0) {
                            AutoTestPaperBean.mlist item = (AutoTestPaperBean.mlist) adapter.getItem(position);
                            AutoTestPaperBean.mlist lastItem = (AutoTestPaperBean.mlist) adapter.getItem(position - 1);
                            if (item.type_id == lastItem.type_id) {
                                Collections.swap(list, position, position - 1);
                                adapter.notifyDataSetChanged();
                            } else {
                                toast("此处只能更改同一题型的顺序哦");
                            }
                        }
                        break;
                    case R.id.tv_down:
                        if (position < list.size() - 1) {
                            AutoTestPaperBean.mlist item = (AutoTestPaperBean.mlist) adapter.getItem(position);
                            AutoTestPaperBean.mlist nextItem = (AutoTestPaperBean.mlist) adapter.getItem(position + 1);
                            if (item.type_id == nextItem.type_id) {
                                Collections.swap(list, position, position + 1);
                                adapter.notifyDataSetChanged();
                            } else {
                                toast("此处只能更改同一题型的顺序哦");
                            }
                        }
                        break;
                    case R.id.tv_delete:
                        adapter.remove(position);
                        break;
                }
            }
        });
    }

    @Override
    protected void formatData() {
        loadData();
    }
    private void loadData(){
        Map<String,Object> map = new HashMap<>();
        map.put("degree_id",""+degree_id);
        map.put("subject_id",""+subject_id);
        map.put("textbook_chapter_id",""+textbook_chapter_id);
        map.put("lore_id",""+lore_id);
        map.put("difficulty",""+difficulty);
        map.put("source_type",""+source_type);
        Map map1 = SPUtils.getInstance(context).parseJSON2Map(questions);
        map.put("questions",map1);
        //map.put("questions",""+questions);
        post("Task/autoCreate",map,BaseBean.class,false);
    }
    @Override
    protected void getBundle(Bundle bundle) {
        degree_id = bundle.getInt("degree_id");
        subject_id = bundle.getInt("subject_id");
        textbook_chapter_id = bundle.getString("textbook_chapter_id");
        lore_id = bundle.getString("lore_id");
        difficulty = bundle.getString("difficulty");
        source_type = bundle.getString("source_type");
        questions = bundle.getString("questions");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_save:
                Map<String,Object> map = new HashMap<>();
                map.put("source_type",""+source_type);
                map.put("subject_id",subject_id);
                map.put("degree_id",degree_id);
                //试卷类型 1自主组卷 2智能组卷 3答题卡试卷
                map.put("paper_type","2");
                map.put("time",""+ed_time.getText().toString());
                if(StringUtils.isNull(textbook_chapter_id)) textbook_chapter_id ="";
                map.put("textbook_chapter_id",""+textbook_chapter_id);
                map.put("paper_name",""+ed_papername.getText().toString());
                List<Map<String,Object>> mapList = new ArrayList<>();
                for(int i = 0;i<parentlist.size();i++ ){
//                    list.addAll(listBean.getList());
                    Map<String,Object> map1 = new HashMap<>();
                    map1.put("type_id",""+parentlist.get(i).type_id);
                    map1.put("type_name",""+parentlist.get(i).type_name);
                    int Score = 0;
                    List<Map<String,String>> mapList_ = new ArrayList<>();
                    for(AutoTestPaperBean.mlist paperBean : parentlist.get(i).list){
                        Map<String,String> map_1 = new HashMap<>();
                        map_1.put("question_id",""+paperBean.question_id);
                        if(StringUtils.isNull(paperBean.score)){ toast("请输入题目分数");return;}
                        map_1.put("score",""+paperBean.score);
                        Score+=Double.parseDouble(paperBean.score);
                        mapList_.add(map_1);
                    }
                    map1.put("score",""+Score);
                    map1.put("questions",mapList_);
                    mapList.add(map1);
                }
                map.put("paper",mapList);
                post("Task/savePaper",map,BaseBean.class,true);
                break;
            case R.id.tv_paper_type:

                break;
            case R.id.tv_paper_jiegou:
                PaperStructurePopupWindow popupWindow = new PaperStructurePopupWindow(AutogeneratingTestPaperListActivity.this, parentlist, new PaperStructurePopupWindow.OnChangePaperTypeStructureListener() {
                    @Override
                    public void onPaperTypeStructureChanged(List<AutoTestPaperBean> singleTypeQuestionBeanList) {
                        if (singleTypeQuestionBeanList != null) {
                            list.clear();
                            for(int i=0;i<singleTypeQuestionBeanList.size();i++){
                                for(AutoTestPaperBean.mlist mlist:singleTypeQuestionBeanList.get(i).list){
                                    mlist.type_id = singleTypeQuestionBeanList.get(i).type_id;
                                }
                                list.addAll(parentlist.get(i).list);
                            }
                            //adapter.setNewData(list);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
                popupWindow.showAsDropDown(tv_paper_jiegou);
//                final TopTestPaperListPopWindow popWindow = new TopTestPaperListPopWindow(this);
//                popWindow.showAtBottom(layoutview);
//                popWindow.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
////                        grade_id = list.get(position).id;
////                        ed_grade.setText(list.get(position).str);
//                        popWindow.dismiss();
//                    }
//                });
                break;
        }
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        RichText.initCacheDir(AutogeneratingTestPaperListActivity.this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RichText.clear(AutogeneratingTestPaperListActivity.this);
    }
}
